﻿using System;
using System.Data.Entity;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormLoaiHangCapNhap : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public int id = 0;
        public FormLoaiHangCapNhap()
        {
            InitializeComponent();
        }

        private void txtTenLoaiHang_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void FormLoaiHangCapNhap_Load(object sender, EventArgs e)
        {
            GetLoaiHangId();
            KiemTra();
        }

        private void bttLuu_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void GetLoaiHangId()
        {
            if (id != 0)
            {
                var t = db.LoaiHangs.Find(id);
                txtDienGiai.Text = t.DienGiai;
                txtTenLoaiHang.Text = t.TenLoaiHang;
            }
            else
            {
                MessageBox.Show("Không tải được dữ liệu!");
                this.Close();
            }
        }

        void Edit()
        {
            try
            {
                var t = db.LoaiHangs.Find(id);
                t.TenLoaiHang = txtTenLoaiHang.Text;
                t.DienGiai = txtDienGiai.Text;
                db.Entry(t).State = EntityState.Modified;
                db.SaveChanges();
                this.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }
        void KiemTra()
        {
            if (txtTenLoaiHang.Text != "")
            {
                bttLuu.Enabled = true;
            }
            else
            {
                bttLuu.Enabled = false;
            }
        }
    }
}
